﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using FRL.DebtChased.Extensions;
using FRL.DebtChased.Repository;
using FRL.DebtChased.Silverlight.Library;
using FRL.DebtChased.WorkflowObjects;

namespace FRL.DebtChased.WorkflowEditor.WorkflowItemEditors
{

	/// <summary>
	/// Declaration of SendSMSCtrl class
	/// Controls for editing the workflow action to send an sms to the debtor, client, agent or solicitor
	/// </summary>
	public partial class SendSMSCtrl : ActionBaseCtrl
	{

		/////////////////////////////////////////////////////////////
		// Construction

		public SendSMSCtrl()
		{

			// Initialize resources
			InitializeComponent();

		}

		/////////////////////////////////////////////////////////////
		// Implementation

		public override List<string> Validate()
		{

			// Declare return variable
			List<string> Ftmp = null;

			// If sending by template...
			if (radContentTemplate.IsChecked ?? false)
			{

				// Get the comm-body
				int iCommBody = ctrlCommBody.CommBodyId;
				if (iCommBody == 0)
				{
					if (Ftmp == null)
						Ftmp = new List<string>();
					Ftmp.Add("Select a message body.");
				}

			}
			else
			{

				// Check for empty free-text
				if (txtFreeText.Text.Length == 0)
				{
					if (Ftmp == null)
						Ftmp = new List<string>();
					Ftmp.Add("Enter a text message.");
				}

			}

			// Return variable
			return Ftmp;

		}

		public override void UpdateControls(WorkflowAction_Base action)
		{

			// Block update events
			this.BlockUpdateEvents = true;
			{

				// Cast the action
				WorkflowAction_SendSMS sendSMSAction = action as WorkflowAction_SendSMS;
#if DEBUG
				Debug.Assert(sendSMSAction != null);
#endif // #if DEBUG

				// Target
				ComboBoxObject.SelectByItemData(cboTarget, (int)sendSMSAction.Target);

				// Set controls - comm body
				ctrlCommBody.Set(sendSMSAction.CommBody);

				// Set controls - radio content
				radContentTemplate.IsChecked = (sendSMSAction.ContentType == WorkflowAction_SendSMS.eContentType.FromTemplate);
				radContentFreeText.IsChecked = (sendSMSAction.ContentType == WorkflowAction_SendSMS.eContentType.FreeText);

				// Set controls - free text
				txtFreeText.Text = sendSMSAction.FreeText;

				// Divert to email
				chkDivertToEmail.IsChecked = (sendSMSAction.DivertToEmail);

			}
			this.BlockUpdateEvents = false;

			// Setup controls
			_setupControls();

		}

		public override void UpdateFromControls(ref WorkflowAction_Base action)
		{

			// If the action needs changing...
			if (!(action is WorkflowAction_SendSMS))
				action = new WorkflowAction_SendSMS();

			// Cast the action
			WorkflowAction_SendSMS sendSMSAction = action as WorkflowAction_SendSMS;
#if DEBUG
			Debug.Assert(sendSMSAction != null);
#endif // #if DEBUG

			// Target
			sendSMSAction.Target = (Definitions.eCommTargetType) cboTarget.GetSelectedItemData();

			// Update object - comm body
			ctrlCommBody.Get(sendSMSAction.CommBody);

			// Update object - content type
			sendSMSAction.ContentType = (radContentTemplate.IsChecked ?? false) ? WorkflowAction_SendSMS.eContentType.FromTemplate : WorkflowAction_SendSMS.eContentType.FreeText;

			// Update object - free text
			sendSMSAction.FreeText = txtFreeText.Text;

			// Divert to email
			sendSMSAction.DivertToEmail = chkDivertToEmail.IsChecked ?? false;

		}

		public override void InitializeControls()
		{

			// Target
			TransportSupport.LoadCommTargetTypeControl(cboTarget, Definitions.eCommTargetType.Debtor);

			// Body control
			ctrlCommBody.InitializeControls(CachedData.Singleton.CommBodies);

			// radio content
			radContentTemplate.IsChecked = true;

			// free text limits
			txtFreeText.MaxLength = Definitions.MaxSMSLength;
			lbSMSLimit.Text = string.Format(lbSMSLimit.Text, Definitions.MaxSMSLength);

			// unblock update events
#if DEBUG
			Debug.Assert(this.BlockUpdateEvents);
#endif // #if DEBUG
			this.BlockUpdateEvents = false;

			// setup controls
			_setupControls();

		}

		/////////////////////////////////////////////////////////////
		// Event handlers

		private new void ActionBaseCtrl_Loaded(object sender, RoutedEventArgs e)
		{

			// Call base-class function
			base.ActionBaseCtrl_Loaded(sender, e);

			// setup controls
			_setupControls();

		}

		private void cboTarget_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{

			// If this control is in a tabbed panel...
			if (m_pnlAddAction != null)
				m_pnlAddAction.UpdateFromCtrl(this);

		}

		private void RadioContent_Checked(object sender, RoutedEventArgs e)
		{

			// Setup controls
			_setupControls();

			// If this control is in a tabbed panel...
			if (m_pnlAddAction != null)
				m_pnlAddAction.UpdateFromCtrl(this);

		}

		private void chkDivertToEmail_Click(object sender, RoutedEventArgs e)
		{

			// If this control is in a tabbed panel...
			if (m_pnlAddAction != null)
				m_pnlAddAction.UpdateFromCtrl(this);

		}

		private void txtFreeText_TextChanged(object sender, TextChangedEventArgs e)
		{

			// If this control is in a tabbed panel...
			if (m_pnlAddAction != null)
				m_pnlAddAction.UpdateFromCtrl(this);

		}

		private void ctrlCommBody_SelectionChanged(object sender, EventArgs e)
		{

			// If this control is in a tabbed panel...
			if (m_pnlAddAction != null)
				m_pnlAddAction.UpdateFromCtrl(this);

		}

		/////////////////////////////////////////////////////////////
		// Operations

		private void _setupControls()
		{

			// Enable/disable controls

			// Show/hide controls
			lbTemplate.Visibility = (radContentTemplate.IsChecked ?? false) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
			ctrlCommBody.Visibility = lbTemplate.Visibility;
			lbFreeText.Visibility = (radContentFreeText.IsChecked ?? false) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
			txtFreeText.Visibility = lbFreeText.Visibility;
			lbSMSLimit.Visibility = lbFreeText.Visibility;

		}

	}

}

